package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.OrderCommentListAdapter;
import com.iexbuy.ihg.adapter.RecommentListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.OrderCommentListObject;
import com.iexbuy.ihg.entity.OrderCommentResponse;
import com.iexbuy.ihg.entity.ReComment;
import com.iexbuy.ihg.entity.ReCommentResponse;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.StringUtil;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeDeCommentFragment extends BaseFragment {

	private static WeDeCommentFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;

	private List<OrderCommentListObject> orderCommentListObjects = new ArrayList<OrderCommentListObject>();
	private int pageNo = 0;
	private OrderCommentListAdapter orderCommentListAdapter;

	private static String shopId;
	private static String itemId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wede_shop_product_comment, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.we_de_product_comment));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		msgListRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				initListData();
			}
			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				loadMore();
			}
		});
		msgListRefreshLayout.autoRefresh();
	}

	private void loadMore() {
		pageNo++;
		initListData();
	}


	public void initListData() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		if (StringUtil.isNotEmpty(shopId)) {
			paramMap.put("shopId", shopId);
		} else if (StringUtil.isNotEmpty(itemId)) {
			paramMap.put("itmeId", itemId);
		}
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_ORDER_COMMENT, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						OrderCommentResponse rep = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), OrderCommentResponse.class);
						if (pageNo == 0) {
							orderCommentListObjects.clear();
						}
						if (rep.getData() != null) {
							orderCommentListObjects.addAll(rep.getData());
						}
						initListView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	private void initListView() {
		if (orderCommentListAdapter == null) {
			orderCommentListAdapter = new OrderCommentListAdapter(mBaseActivity, orderCommentListObjects);
			listView.setAdapter(orderCommentListAdapter);
		}
//		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//			@Override
//			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//				WeDeCommentDetailFragment.setReCommentObject(reComments.get(i));
//				CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeCommentDetailFragment.class);
//			}
//		});
		orderCommentListAdapter.notifyDataSetChanged();
	}

	public static void setItemId(String itemId) {
		WeDeCommentFragment.itemId = itemId;
		WeDeCommentFragment.shopId = null;
	}

	public static void setShopId(String shopId) {
		WeDeCommentFragment.itemId = null;
		WeDeCommentFragment.shopId = shopId;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static WeDeCommentFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
